﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Globalization;
using Google.GData.YouTube;
using Resources;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;
using Google.GData.Client;
using Google.GData.Photos;
using FlickrNet;

public partial class _Page : System.Web.UI.Page
{
    public string CityName { get; set; }

    public string CountryName { get; set; }

    public string SubWord { get; set; }


    public string SearchPhrase
    {
        get
        {
            string result;
            if (!string.IsNullOrEmpty(CountryName))
            {
                result = CountryName;
                if (!string.IsNullOrEmpty(CountryName))
                {
                    result += " " + CityName;
                }
                if (!string.IsNullOrEmpty(CountryName))
                {
                    result += " " + SubWord;
                }
            }
            else
            {
                result = "Tourism And Travelling";
            }
            return result;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (string key in Request.QueryString)
            {
                switch (key.ToLower())
                {
                    case "country": this.CountryName = Request.QueryString[key]; break;
                    case "city": this.CityName = Request.QueryString[key]; break;
                    case "topic": this.SubWord = Request.QueryString[key]; break;
                }
            }
            MainWebContent.SearchPhrase = SearchPhrase;
            this.Title = GenerateTitle();
            if (!string.IsNullOrEmpty(CountryName))
            {
                LoadCities();
                LoadTopics();
            }

        }

        //Set the title

    }

    private void LoadTopics()
    {
        List<SubWord> topics;
        if (!string.IsNullOrEmpty(this.CityName))
        {
            topics = SubWordsManager.GetCitySubWords();
        }
        else
        {
            topics = SubWordsManager.GetCountrySubWords();
        }

        TopicsListView.DataSource = topics;
        TopicsListView.DataBind();
    }

    protected void TopicsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem data = e.Item as ListViewDataItem;
            SubWord topic = data.DataItem as SubWord;
            HyperLink topicLink = e.Item.FindControl("TopicLink") as HyperLink;
            topicLink.Text = topic.TitleString;

            if (!string.IsNullOrEmpty(this.CityName))
            {
                topicLink.NavigateUrl = string.Format("~/page.aspx?Country={0}&City={1}&topic={2}", this.CountryName, this.CityName, topic.KeyWord);
            }
            else
            {
                topicLink.NavigateUrl = string.Format("~/page.aspx?Country={0}&topic={1}", this.CountryName, topic.KeyWord);
            }

        }
    }

    private string GenerateTitle()
    {
        StringBuilder result = new StringBuilder();
        result.Append(this.SearchPhrase);
        result.Append(" - Latest ");
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
        result.Append(" News, Blogs, Pictures And Videos");
        //result.Append(DateTime.Now.ToString("MMMM yyyy"));
        string actualResult = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(result.ToString());
        return actualResult;
    }


    private void LoadCities()
    {
        List<string> models = CountryHelper.GetCitiesByCountry(this.CountryName, 50);
        ModelsListView.DataSource = models;
        ModelsListView.DataBind();
    }


    protected void ModelsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem data = e.Item as ListViewDataItem;
            string model = data.DataItem as string;
            HyperLink TrendLink = e.Item.FindControl("ModelLink") as HyperLink;
            TrendLink.Text = model;
            TrendLink.NavigateUrl = string.Format("~/page.aspx?Country={0}&City={1}", this.CountryName, model);
        }
    }




}

